import Vue from 'vue'
import * as Util from '../../components/Util'

let MachineChargeService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.MachineChargeService = Vue.prototype.$MachineChargeService = MachineChargeService
  },

  // 按户收费 (只存一条收费记录)
  marchineChargeUser (model) {
    console.log('按户收费，，', model)
  },
  // 按表收费
  marchineCharge (model, row) {
    console.log('保存收费。。。', model, row)
    let data = {
      f_userinfo_id: row.f_userinfo_id + '',
      version: row.userinfo_version,
      f_balance: model.f_curbalance,
      overdues: {},
      handplan_ids: []
    }
    let delaypay = 0
    let preamount = 0
    let pregas = 0
    let tablebase = 0
    let last_tablebase = 0
    if (model.rows.length > 0) {
      model.rows.forEach((item, index) => {
        if (index === 0) {
          last_tablebase = item.f_last_tablebase
        }
        if (index === model.rows.length - 1) {
          tablebase = item.f_tablebase
        }
        data.handplan_ids.push(item.handplan_id)
        delaypay = delaypay + item.overdue
        preamount = preamount + item.f_oughtfee
        pregas = pregas + item.f_oughtamount
      })
    } else {
      last_tablebase = row.f_meter_base
      tablebase = row.f_meter_base
    }
    console.log('row.times', row.f_times)
    data.overdues = {
      f_user_id: row.f_user_id,
      f_times: row.f_times + 1,
      // handplan_id: item.handplan_id,
      userinfo: row.f_userinfo_id,
      f_operat_type: '机表收费',
      f_describe: `${Util.f.name}对用户${row.f_user_name}进行机表收费操作`,
      f_state: '有效',
      f_delaypay: delaypay,
      f_charge_type: '机表收费',
      f_preamount: preamount,
      f_pregas: pregas,
      f_tablebase: tablebase,
      f_last_tablebase: last_tablebase,
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname,
      f_bill_style: model.f_print[0]
    }
    Object.assign(data.overdues, Vue.CommonService.buildSellinggas(model, row), data.overdues)
    Vue.console.info(data, 'MachineChargeService')
    console.log('收费logic中传的data', data)
    return Vue.resetpost('rs/logic/machineCharge', {data: data}, {resolveMsg: null, rejectMsg: '收费失败失败'})
  }
}

export default MachineChargeService
